package com.jarvis.app.car;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.jarvis.app.car.common.MessageResult;
import com.jarvis.app.car.constant.AppConstant;
import com.jarvis.app.car.util.ServiceManager;
import com.jarvis.app.car.util.ThreadPoolManager;
import com.mysql.jdbc.StringUtils;

public class AddTicketActivity extends AppCompatActivity {


    private ImageView iv_back;

    private EditText et_issue;

    private Button bt_submit;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message message) {
            MessageResult<Void> messageResult = (MessageResult<Void>) message.obj;
            if (!messageResult.isJudgeSuccess()) {
                Toast.makeText(AddTicketActivity.this, messageResult.getMsg(), Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(AddTicketActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                setResult(Integer.MIN_VALUE);
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_add_ticket);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        iv_back = findViewById(R.id.iv_back);
        et_issue = findViewById(R.id.et_issue);
        bt_submit = findViewById(R.id.bt_submit);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(Integer.MIN_VALUE);
                finish();
            }
        });

        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String issue = et_issue.getText().toString().trim();
                if (StringUtils.isNullOrEmpty(issue)) {
                    Toast.makeText(AddTicketActivity.this, "问题不能为空", Toast.LENGTH_SHORT).show();
                }

                ThreadPoolManager.threadPoolExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        MessageResult<Void> one = ServiceManager.ticketService.create(AppConstant.ACCOUNT_ID, issue);
                        Message msg = Message.obtain();
                        msg.obj = one;
                        handler.sendMessage(msg);
                    }
                });
            }
        });


    }
}